<template>
  <div>
    <van-search
      v-model="keyWord"
      show-action
      label="地址"
      placeholder="请输入搜索关键词"
      background="skyblue"
      @search="onSearch"
    >
    <template #action>
        <div @click="onSearch">搜索</div>
      </template>
      </van-search>
      <div class="hisWrap">
        <h3>搜索记录 :</h3>
        <div class="histories">
          <div class="his" v-if="!histories.length">暂无搜索记录</div>
          <div class="his" @click="clickHis(his)" v-for="his in histories" :key="his">
            {{ his }}
          </div>
        </div>
      </div>
  </div>
</template>

<script>
export default {
  name: 'search',
  data () {
    return {
      keyWord: '',
      histories: localStorage.getItem('his') ? JSON.parse(localStorage.getItem('his')) : []
    }
  },
  methods: {
    clickHis (word) {
      this.keyWord = word
      this.onSearch()
    },
    onSearch () {
      let his = localStorage.getItem('his') ? JSON.parse(localStorage.getItem('his')) : []
      his.unshift(this.keyWord)
      his = [...new Set(his)]
      if (his.length >= 3) {
        his.length = 3
      }
      localStorage.setItem('his', JSON.stringify(his))
      this.$router.push({
        path: '/searchResult',
        query: {
          keyword: this.keyWord
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.van-search__label{
  font-size: 16px;
  padding-left: 0;
  color: skyblue;
}
h3{
  padding-top: 15px;
  padding-left: 15px;
  font-size: 16px;
  color: #666;
}
.histories .his[data-v-7d459965]{
  background-color: rgba(160, 215, 240, 0.5);
  margin-left: 15px;
  margin-top: 15px;
  font-size: 16px;
}
.van-field__control{
  font-size: 14px;
}
.van-search__action{
  font-size: 16px;
  color: #fff;
}
.histories{
  display: flex;
  flex-wrap: wrap;
  .his{
    padding: 5px 15px;
    background-color: #fff;
    margin-top: 10px;
    margin-left: 10px;
  }
}
</style>
